package com.qd.panda.carpark;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.panda.domain.entity.carpark.CarParkDoorpostDTO;
import com.qd.panda.service.park.CarParkDoorpostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * WEB_设置门岗
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/carParkDoorpost/")
@Slf4j
public class CarParkDoorpostController {
    private final CarParkDoorpostService carParkDoorpostService;

    /**
     * 添加设置门岗
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/save")
    public CarParkDoorpostDTO save(@Validated({DefaultGroup.class}) @RequestBody CarParkDoorpostDTO dto) {
        dto.setAddTime(LocalDateTime.now());
        return carParkDoorpostService.save(dto);
    }

    /**
     * 修改设置门岗
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PutMapping(value = "/update")
    public CarParkDoorpostDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody CarParkDoorpostDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        return carParkDoorpostService.update(dto);
    }

    /**
     * 删除设置门岗
     *
     * @param delDTO 主键，多个用分号隔开
     * @return 页面对象
     */
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            carParkDoorpostService.removeById(ids.get(0));
        } else {
            carParkDoorpostService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取设置门岗
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public CarParkDoorpostDTO getById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return carParkDoorpostService.getByIdDto(id);
    }

    /**
     * 设置门岗列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<CarParkDoorpostDTO> page(CarParkDoorpostDTO dto) {
        return carParkDoorpostService.page(PageUtil.buildPage(dto), dto);
    }
}